#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
客户可选择是否采用代理的方式爬取网站内容
代理可通过list表单有多个选择
'''
import urllib2
import random
proxyswitch = True


ua = {
    "Connection" : "keep-alive",
    #"Upgrade-Insecure-Requests" : "1",
    "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
    "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    #"Referer" : "http://guide.renren.com/fillinfonew",
    #"Accept-Encoding" : "gzip, deflate",
    "Accept-Language" : "zh-CN,zh;q=0.9",
    #"Cookie" : "anonymid=jl92cv7e3cplh5; _r01_=1; depovince=BJ; JSESSIONID=abctmOWo0uXt99T8MEHBw; __guid=238633222.4184282686151254500.1541381189280.138; monitor_count=1; ick_login=2c9ae5fe-258c-4962-b80e-829f87d4ca3e; jebe_key=f39e0986-d1f2-4c97-ad49-3dafc7726836%7Ccfcd208495d565ef66e7dff9f98764da%7C1541381192119%7C0%7C1541381191582; ick=16ea19c1-6e1a-4a6c-bb03-6f43a99ffda8; t=8215750971474f9939e7707c8d13c1d20; societyguester=8215750971474f9939e7707c8d13c1d20; id=968577790; xnsid=1e84080c; XNESSESSIONID=e3af02505b8d; WebOnLineNotice_968577790=1; jebecookies=118e4dfa-c8f2-406c-a10d-e254b738d857|||||"
}

url = "http://www.baidu.com/"
proxy_list=[
{"http" : "221.2.155.35:8060"}#最好用刚刚出的免费代理时间

]

proxy_ip = random.choice(proxy_list)

if proxyswitch:
    handler_proxy = urllib2.ProxyHandler(proxy_ip)
else:
    handler_proxy = urllib2.ProxyHandler({})


opener = urllib2.build_opener(handler_proxy)

request = urllib2.Request(url, headers=ua)

response = opener.open(request)

print(response.read())


